<template>
  <el-dialog :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称" :disabled="dataForm.isDel == 1"></el-input>
      </el-form-item>
      <el-form-item label="押金" prop="isDeposit"><el-switch v-model="dataForm.isDeposit" :active-value="1" :inactive-value="0"></el-switch></el-form-item>
      <el-form-item label="结账" prop="isClosing"><el-switch v-model="dataForm.isClosing" :active-value="1" :inactive-value="0"></el-switch></el-form-item>
      <el-form-item label="排序" prop="sort"><el-input v-model="dataForm.sort" placeholder="排序"></el-input></el-form-item>
      <el-form-item label="字典" prop="type" v-show="false">
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state"><el-switch v-model="dataForm.state" :active-value="1" :inactive-value="0" active-text="启用" class="switch" inactive-text="禁用"></el-switch></el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      options: [
        {
          value: "0",
          label: '默认'
        },
        {
          value: '1',
          label: '银行卡'
        },
        {
          value: '2',
          label: "储值卡"
        },
        {
          value: '3',
          label:"AR帐"
        },
        {
          value: '4',
          label:"储值卡"
        },
        {
          value: '5',
          label:"协议单位"
        }
      ],
      visible: false,
      dataForm: {
        id: 0,
        sellerId: '',
        name: '',
        isDeposit: 1,
        isDel: 0,
        isClosing: 1,
        sort: 0,
        type: '0',
        creatTime: '',
        state: 1
      },
      dataRule: {
        sellerId: [{ required: true, message: '商家不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        isDeposit: [{ required: true, message: '押金（0禁用1启用）不能为空', trigger: 'blur' }],
        isClosing: [{ required: true, message: '结账（0禁用1启用）不能为空', trigger: 'blur' }],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        creatTime: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
        state: [{ required: true, message: '状态（0禁用1启用）不能为空', trigger: 'blur' }]
      }
    };
  },
  methods: {
    init(id) {
      Object.assign(this.$data, this.$options.data());
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroompaytype/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.sellerId = data.tHotelFloorRoomPayType.sellerId;
              this.dataForm.name = data.tHotelFloorRoomPayType.name;
              this.dataForm.isDeposit = data.tHotelFloorRoomPayType.isDeposit;
              this.dataForm.isClosing = data.tHotelFloorRoomPayType.isClosing;
              this.dataForm.isDel = data.tHotelFloorRoomPayType.isDel;
              this.dataForm.sort = data.tHotelFloorRoomPayType.sort;
              this.dataForm.creatTime = data.tHotelFloorRoomPayType.creatTime;
              this.dataForm.type = data.tHotelFloorRoomPayType.type;
              this.dataForm.state = data.tHotelFloorRoomPayType.state;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/hotel/thotelfloorroompaytype/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              sellerId: this.dataForm.sellerId,
              name: this.dataForm.name,
              isDeposit: this.dataForm.isDeposit,
              isClosing: this.dataForm.isClosing,
              sort: this.dataForm.sort,
              creatTime: this.dataForm.creatTime,
              type: this.dataForm.type,
              state: this.dataForm.state
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: this.$t('Common.OperationSuccessful'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
